package fr.afcepf.ai90.venteenligne.data;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.sql.DataSource;

import fr.afcepf.ai90.util.SimpleDataSource;
import fr.afcepf.ai90.venteenligne.entity.Personne;

public class DataPersonneImpl implements IDataPersonne{


    private final String reqVerifConnection = "SELECT * FROM personne p WHERE p.mail = ? AND p.mdp = ?";
    /**
     * Constante definissant l'indice du parametre
     * mail dans la requete de verifConnection
     */
    private final int indiceParamMail = 1;
    /**
     *  Constante definissant l'indice du parametre
     * mdp dans la requete de verifConnection
     */
    private final int indiceParamMdp = 2;

    private final String colonneID = "id";
    private final String colonneNom = "nom";
    private final String colonnePrenom = "prenom";
    private final String colonneDateNaissance = "date_Naissance";
    private final String colonneMail = "mail";
    private final String colonneMdp = "mdp";
   

    @Override
    public Personne verifConnection(String paramMail, String paramMdp) {
        
        DataSource dc = new SimpleDataSource();
        Personne retour = null;

        try {
            Connection cnx = dc.getConnection();
            PreparedStatement pstmt = cnx.prepareStatement(reqVerifConnection);
            pstmt.setString(indiceParamMail, paramMail);
            pstmt.setString(indiceParamMdp, paramMdp);
            ResultSet rs = pstmt.executeQuery();
            if (rs.next()) {
                retour = new Personne( 
                        rs.getInt(colonneID),
                        rs.getString(colonneNom),
                        rs.getString(colonnePrenom),
                        rs.getDate(colonneDateNaissance),
                        rs.getString(colonneMail),
                        rs.getString(colonneMdp)
                       );
            }

        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        return retour;
    }


}
